* ======================================
* ------------- Split prWin ------------
* ======================================


	use $dta\EstimationSample.dta, clear

	keep Voter_* year Fam? Work? Imm?
		
	* Match voters to politicians using max rank, not min distance
	run $prog/prog_match $dta/PanelVoters $dta/PanelPoliticians Fam1 idmatch rank
	run $prog/prog_match $dta/PanelVoters $dta/PanelPoliticians Fam2 idmatch rank
	run $prog/prog_match $dta/PanelVoters $dta/PanelPoliticians Work1 netmatch rank
	run $prog/prog_match $dta/PanelVoters $dta/PanelPoliticians Work2 netmatch rank
	run $prog/prog_match $dta/PanelVoters $dta/PanelPoliticians Imm1 netmatch rank
	run $prog/prog_match $dta/PanelVoters $dta/PanelPoliticians Imm2 netmatch rank

	
	* Import win predictions
	foreach net in Fam1 Fam2 Work1 Work2 Imm1 Imm2{
		rename `net'Pol_id Polit_id
		merge m:1 Polit_id year using $dta/prWin.dta, nogen ///
			keepusing(Polit_prWin) keep(master matched)	
		rename Polit_prWin `net'Pol_prWin
		rename Polit_id `net'Pol_id
	}

	
* Estimate models
* ===============

	egen idbsu = group(Voter_id Voter_bsu)
	gen period = 1*(year == 2019)
	xtset idbsu period
	estimates clear


	foreach net in Fam1 Fam2 Work1 Work2 Imm1 Imm2{
	
		preserve
	
		* Set sample
		keep if `net' == 1 

		gen network_r4 = 1*(`net'Any == 1 & `net'Pol_prWin < 0.01)
		gen network_r3 = 1*(`net'Any == 1 & `net'Pol_prWin >= 0.01 & `net'Pol_prWin < 0.1)
		gen network_r2 = 1*(`net'Any == 1 & `net'Pol_prWin >= 0.1 & `net'Pol_prWin < 0.5)
		gen network_r1 = 1*(`net'Any == 1 & `net'Pol_prWin >= 0.5 & `net'Pol_prWin <= 1)
		gen networkXknr_r4 = 1*(`net'Same == 1 & `net'Pol_prWin < 0.01)
		gen networkXknr_r3 = 1*(`net'Same == 1 & `net'Pol_prWin >= 0.01 & `net'Pol_prWin < 0.1)
		gen networkXknr_r2 = 1*(`net'Same == 1 & `net'Pol_prWin >= 0.1 & `net'Pol_prWin < 0.5)
		gen networkXknr_r1 = 1*(`net'Same == 1 & `net'Pol_prWin >= 0.5 & `net'Pol_prWin <= 1)	
		
		if substr("`net'",1,3) == "Imm"{
			replace network_r2 = 1*(network_r2 == 1 | network_r1 == 1)
			replace networkXknr_r2 = 1*(networkXknr_r2 == 1 | networkXknr_r1 == 1)
			drop network_r1 networkXknr_r1
		}
		
		eststo Rank_`net': xtreg Voter_turnout network_r* networkXknr_r*  i.period, ///
			i(idbsu) fe cluster(Voter_bsu)
		
		restore
	}



* Export coefficient plot
* =======================
	
	* PANEL A
	coefplot ///
		(Rank_Fam1, mlcol(gs2) mcol(white) msize(medium) msym(O)) || ///
		(Rank_Fam2, mlcol(gs2) mcol(white) msize(medium) msym(O)) ||  ///
		(Rank_Work1, msym(O)) || (Rank_Work2, msym(O)) || ///
		(Rank_Imm1, msym(O)) || (Rank_Imm2,  msym(O)), ///
		keep(network_*) vertical omitted recast(connected) grid(none) ///
		order( *_r4 *_r3 *_r2 *_r1) ///
		ciopts(recast(rarea) fcol(black%15) lcol(black%40)) ///
		byopts(title("Panel A: Any District", size(medsmall)) rows(1)) ///
		xlab(1 "4" 2 "3" 3 "2" 4 "1", labsize(medsmall)) ///
		ylab(-0.05(0.05)0.10, labsize(medsmall)) ///
		ytitle(Coefficient estimate, size(small)) ///
		xtitle("Candidate viability (1 is {it:strong})", size(small)) ///
		bylabels(`"Family "(close)""' `"Family "(extended)""' ///
			`"Co-workers "(age-estbl.)""' `"Co-workers "(estbl.)""' ///
			`"Immigrants "(3-digit)""' `"Immigrants "(2-digit)""') ///
		subtitle(,bcolor(none)) ///
		yline(0, lpattern(solid) lcol(cranberry)) ///
		legend(row(1) size(medium)) ///
		scheme(plotplain) xsize(8) name(panelA, replace)
	
	* PANEL B
	coefplot ///
		(Rank_Fam1, mlcol(gs2) mcol(white) msize(medium) msym(O)) || ///
		(Rank_Fam2, mlcol(gs2) mcol(white) msize(medium) msym(O)) ||  ///
		(Rank_Work1, msym(O)) || (Rank_Work2, msym(O)) || ///
		(Rank_Imm1, msym(O)) || (Rank_Imm2,  msym(O)), ///
		keep(networkXknr_*) vertical omitted recast(connected) grid(none) ///
		order( *_r4 *_r3 *_r2 *_r1 ) ///
		ciopts(recast(rarea) fcol(black%15) lcol(black%40)) ///
		byopts(title("Panel B: Same District", size(medsmall)) rows(1)) ///
		xlab(1 "4" 2 "3" 3 "2" 4 "1", labsize(medsmall)) ///
		ylab(-0.05(0.05)0.10, labsize(medsmall)) ///
		ytitle(Coefficient estimate, size(small)) ///
		xtitle("Candidate viability (1 is {it:strong})", size(small)) ///
		bylabels(`"Family "(close)""' `"Family "(extended)""' ///
			`"Co-workers "(age-estbl.)""' `"Co-workers "(estbl.)""' ///
			`"Immigrants "(3-digit)""' `"Immigrants "(2-digit)""') ///
		subtitle(,bcolor(none)) ///
		yline(0, lpattern(solid) lcol(cranberry)) ///
		legend(row(1) size(medium)) ///
		scheme(plotplain) xsize(8) name(panelB, replace)

	
	* COMBINE
	graph combine panelA panelB, scheme(plotplain) col(1)
	graph export $figs/SplitPrWin.pdf, replace